 package com.example.home.attorney.activity;

 import android.os.Bundle;
 import android.view.View;
 import android.widget.AdapterView;
 import android.widget.ArrayAdapter;
 import android.widget.ImageView;
 import android.widget.Spinner;
 import android.widget.Toast;

 import androidx.appcompat.app.AppCompatActivity;
 import androidx.recyclerview.widget.LinearLayoutManager;
 import androidx.recyclerview.widget.RecyclerView;

 import com.example.home.R;
 import com.example.home.attorney.adapter.MeConsultAdapter;
 import com.example.home.attorney.bean.MeConsultBean;
 import com.example.home.tool.OKHttpTool;

 // 定义排序方式的枚举类型
 enum MeConsultSortType {
     NOTDISPOSE(R.string.sort_not_dispose),
     ISDISPOSE(R.string.sort_is_dispose);


     private final int displayMeResId;

     MeConsultSortType(int displayMeResId) {
         this.displayMeResId = displayMeResId;
     }

     public String getDisplayMeName(AppCompatActivity context) {
         return context.getString(displayMeResId);
     }
 }


public class MeConsult extends AppCompatActivity {
    private MeConsultSortType currentSortType = MeConsultSortType.NOTDISPOSE;
    private ImageView back;
    private Spinner sortSpinner;
    private RecyclerView rcvList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_me_consult);
        initView();
        // 初始化Spinner
        initSpinner();
    }

    private void initSpinner() {
        Spinner sortSpinner = findViewById(R.id.sortSpinner);

        // 创建排序方式的适配器
        ArrayAdapter<CharSequence> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        // 添加枚举值的中文显示名称到适配器
        for (MeConsultSortType meConsultSortType : MeConsultSortType.values()) {
            adapter.add(meConsultSortType.getDisplayMeName(this));
        }

        // 设置适配器
        sortSpinner.setAdapter(adapter);

        // 设置选择监听器
        sortSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                // 用户选择的排序方式
                currentSortType = MeConsultSortType.values()[position];

                // 根据新的排序方式刷新律师列表
                loadLawyerList(currentSortType);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parentView) {
                // 未选择时的处理
            }
        });
    }

    private void loadLawyerList(MeConsultSortType currentSortType) {

        // 根据排序方式加载律师列表数据
        switch (currentSortType) {
            case NOTDISPOSE:
                initLawyerList();
                break;
            case ISDISPOSE:
                loadExperienceList();
                break;
        }

    }

    private void initLawyerList() {
        // 初始化律师列表
        OKHttpTool.doNetRequest("prod-api/api/lawyer-consultation/legal-advice/list",
                OKHttpTool.GET,
                MeConsultBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        MeConsultBean meConsultBean = (MeConsultBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                bindList(meConsultBean);
                            }
                        });
                    }


                    @Override
                    public void onFailure(String msg) {
                        // 处理请求失败的情况
                    }
                });
    }

    private void bindList(MeConsultBean meConsultBean) {
        RecyclerView recyclerView = findViewById(R.id.rcv_list);
        MeConsultAdapter meConsultAdapter = new MeConsultAdapter(MeConsult.this,meConsultBean.getRows());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MeConsult.this);
        recyclerView.setAdapter(meConsultAdapter);
        recyclerView.setLayoutManager(linearLayoutManager);
    }

    private void loadExperienceList() {
        Toast.makeText(MeConsult.this,"暂无已受理数据",Toast.LENGTH_SHORT).show();
    }

    private void initView() {

        back = findViewById(R.id.back);
        sortSpinner = findViewById(R.id.sortSpinner);
        rcvList = findViewById(R.id.rcv_list);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MeConsult.this.finish();
            }
        });
    }
}